package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.bean.LoginBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.SP;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.tv_forget_psd)
    TextView tvForgetPsd;
    @Bind(R.id.tv_regist)
    TextView tvRegist;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_login_phone)
    EditText etLoginPhone;
    @Bind(R.id.et_login_pwd)
    EditText etLoginPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_login, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        etLoginPhone.setText("qqqqqq");
        etLoginPwd.setText("123456");
    }

    private void initEvent() {
        btnLogin.setOnClickListener(this);
        tvForgetPsd.setOnClickListener(this);
        tvRegist.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_forget_psd:
                Intent intent=new Intent(this,ChangePsdActivity.class);
                intent.putExtra("what","forgetpwd");
                startActivity(intent);
                break;
            case R.id.tv_regist:
                startActivity(new Intent(this, RegistActivity.class));
                break;
            case R.id.btn_login:
                if(Utils.isEmpty(etLoginPhone.getText().toString())){
                    T.showShort(this,getString(R.string.str_username_notnull));
                }else if(Utils.isEmpty(etLoginPwd.getText().toString())){
                    T.showShort(this,getString(R.string.str_pwd_notnull));
                }else{
                    login();
                }

                break;
        }

    }

    private void login() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline = Utils.getTimeline();
        String sign = Utils.md5(timeline);
        String psd = Utils.base64Encode(etLoginPwd.getText().toString());
        L.d("timeline", timeline);
        L.d("sign", sign);
        L.d("psd", psd);
        getTestApi().login(timeline, sign, etLoginPhone.getText().toString(), psd)
                .compose(this.<LoginBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Loading.dismiss();
                        T.showLong(LoginActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        Loading.dismiss();
                        L.d(loginBean.getReturn_code() + "");
                        if (Utils.checkResultCode(loginBean.getReturn_code())) {
                            SP.put(LoginActivity.this,"id",loginBean.getData().getId());
                            SP.put(LoginActivity.this,"username",loginBean.getData().getUsername());
                            SP.put(LoginActivity.this,"phone",loginBean.getData().getPhone());
                            startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                            finish();
                        } else {
                            T.showLong(LoginActivity.this, loginBean.getMsg());
                        }
                    }
                });
    }
}
